//
//  Utitlity.m
//  wiimudemo_conf
//
//  Created by 赵帅 on 14-3-28.
//  Copyright (c) 2014年 wiimu. All rights reserved.
//

#import "Utitlity.h"

@implementation Utitlity

+(void)commonHTTPMethod:(NSString *)url responseType:(responseType)type success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    if(type == jsonType)
    {
        operation.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    else if(type == stringType)
    {
        operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    else if(type == xmlType)
    {
        operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
    }
    
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"application/x-javascript",nil];
    [operation setCompletionBlockWithSuccess:success failure:failure];
    [operation start];
}

+ (void)postHttpMethod:(NSString *)url data:(NSDictionary *)dataDic success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:dataDic success:success
          failure:failure];
}


+ (NSString *)httpURLEncode:(NSString *)plainText {
    ASIFormDataRequest *formDataRequest = [ASIFormDataRequest requestWithURL:nil];
    return [formDataRequest encodeURL:plainText];
}

#pragma mark - 判断ipUrl是否存在
+ (NSString *)judgeipUrl
{
    NSUserDefaults*userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *deviceStr = [userDefaults objectForKey:@"device"];
    BOOL isUrl = NO;
    NSString *ipUrl = nil;
    for (NSDictionary *dic in [GlobalInfo sharedInstance].upnpArray)
    {
        if ([dic[@"uuid"] isEqualToString:deviceStr])
        {
            isUrl = YES;
            NSString *ipUrlStr = dic[@"baseURL"];
            NSURL *url = [NSURL URLWithString:ipUrlStr];
            ipUrl = url.host;
        }
    }
    
    if (!isUrl)
    {
        ipUrl = nil;
    }
    
    if (![GlobalInfo isRunOnDevice]) {
        ipUrl = [NSString stringWithFormat:@"%@:%ld", [GlobalInfo sharedInstance].tempHttpHost, [GlobalInfo sharedInstance].tempHttpPort];
    }
    return ipUrl;
}

@end
